﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using Word = Microsoft.Office.Interop.Word;
using System.Threading;
using Osobe;
using SkolskaUstanova;
using MolbeIPotvrde;

namespace ElektronskiDnevnik
{
    public partial class PregledZahtjeva : Form
    {
        MySqlConnection conn;
        MySqlDataReader rdr = null;
        String JMBG, ucenik;
        List<PotvrdaMolba> molbeCekanje;
        List<PotvrdaMolba> molbeOdobrene;
        List<PotvrdaMolba> molbeOdbijene;
        int brojac1, brojac2, brojac3;
        public PregledZahtjeva(String maticni, String u, MySqlConnection c)
        {
            InitializeComponent();
            conn = c;
            ucenik = u;
            JMBG = maticni;
            brojac1 = brojac2 = brojac3 = 1;
            molbeCekanje = new List<PotvrdaMolba>();
            molbeOdbijene = new List<PotvrdaMolba>();
            molbeOdobrene = new List<PotvrdaMolba>();
            MySqlCommand cmd = new MySqlCommand("SELECT * FROM PotvrdaMolba WHERE Podnosioc = '" + JMBG + "' AND Ucenik = '" + ucenik + "';", conn);
            try
            {
                rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    if (Convert.ToInt32(rdr["Tip"]) == 2 && Convert.ToInt32(rdr["Rijesenje"]) == 1)
                    {
                        Molba molba = new Molba(Convert.ToString(rdr["BrojProtokola"]), Convert.ToDateTime(rdr["Datum"]), Convert.ToInt32(rdr["Rijesenje"]), Convert.ToString(rdr["Svrha"]), Convert.ToString(rdr["Ucenik"]), Convert.ToString(rdr["Podnosioc"]),2);
                        molbeCekanje.Add(molba);
                    }
                    if (Convert.ToInt32(rdr["Tip"]) == 1 && Convert.ToInt32(rdr["Rijesenje"]) == 1)
                    {
                        RedovnoSkolovanje molba = new RedovnoSkolovanje(Convert.ToString(rdr["BrojProtokola"]), Convert.ToDateTime(rdr["Datum"]), Convert.ToInt32(rdr["Rijesenje"]), Convert.ToString(rdr["Svrha"]), Convert.ToString(rdr["Ucenik"]), Convert.ToString(rdr["Podnosioc"]),1);
                        molbeCekanje.Add(molba);
                    }
                    if (Convert.ToInt32(rdr["Tip"]) == 2 && Convert.ToInt32(rdr["Rijesenje"]) == 2)
                    {
                        Molba molba = new Molba(Convert.ToString(rdr["BrojProtokola"]), Convert.ToDateTime(rdr["Datum"]), Convert.ToInt32(rdr["Rijesenje"]), Convert.ToString(rdr["Svrha"]), Convert.ToString(rdr["Ucenik"]), Convert.ToString(rdr["Podnosioc"]),2);
                        molbeOdobrene.Add(molba);
                    }
                    if (Convert.ToInt32(rdr["Tip"]) == 1 && Convert.ToInt32(rdr["Rijesenje"]) == 2)
                    {
                        RedovnoSkolovanje molba = new RedovnoSkolovanje(Convert.ToString(rdr["BrojProtokola"]), Convert.ToDateTime(rdr["Datum"]), Convert.ToInt32(rdr["Rijesenje"]), Convert.ToString(rdr["Svrha"]), Convert.ToString(rdr["Ucenik"]), Convert.ToString(rdr["Podnosioc"]),1);
                        molbeOdobrene.Add(molba);
                    }
                    if (Convert.ToInt32(rdr["Tip"]) == 2 && Convert.ToInt32(rdr["Rijesenje"]) == 3)
                    {
                        Molba molba = new Molba(Convert.ToString(rdr["BrojProtokola"]), Convert.ToDateTime(rdr["Datum"]), Convert.ToInt32(rdr["Rijesenje"]), Convert.ToString(rdr["Svrha"]), Convert.ToString(rdr["Ucenik"]), Convert.ToString(rdr["Podnosioc"]),2);
                        molbeOdbijene.Add(molba);
                    }
                    if (Convert.ToInt32(rdr["Tip"]) == 1 && Convert.ToInt32(rdr["Rijesenje"]) == 3)
                    {
                        RedovnoSkolovanje molba = new RedovnoSkolovanje(Convert.ToString(rdr["BrojProtokola"]), Convert.ToDateTime(rdr["Datum"]), Convert.ToInt32(rdr["Rijesenje"]), Convert.ToString(rdr["Svrha"]), Convert.ToString(rdr["Ucenik"]), Convert.ToString(rdr["Podnosioc"]),1);
                        molbeOdbijene.Add(molba);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Greska", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (rdr != null)
                    rdr.Close();
            }
            
        }

        private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
        {
            Brush BackBrush = new SolidBrush(Color.AntiqueWhite);
            Brush ForeBrush = new SolidBrush(Color.Salmon);
            Font TabFont = new Font("Segoe Print", 10, FontStyle.Regular);
            string TabName = this.tabControl1.TabPages[e.Index].Text;
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;
            e.Graphics.FillRectangle(BackBrush, e.Bounds);
            Rectangle r = e.Bounds;
            r = new Rectangle(r.X, r.Y + 3, r.Width, r.Height - 3);
            e.Graphics.DrawString(TabName, TabFont, ForeBrush, r, sf);
            sf.Dispose();
            if (e.Index == this.tabControl1.SelectedIndex)
            {
                TabFont.Dispose();
                BackBrush.Dispose();
            }
            else
            {
                BackBrush.Dispose();
                ForeBrush.Dispose();
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            var rez = MessageBox.Show("Sigurni ste da se zelite otkazati zahtjev?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (rez == DialogResult.No) return;
            else
            {
                int otkazano = 4;
                MySqlCommand cmd = new MySqlCommand("UPDATE PotvrdaMolba SET Rijesenje = " + otkazano + " WHERE BrojProtokola = '" + (molbeCekanje[brojac1 - 1]).BrojProtokola + "';", conn);
                cmd.ExecuteNonQuery();
                molbeCekanje.Remove(molbeCekanje[brojac1 - 1]);
                if (brojac1 != 1 || (brojac1 == 1 && molbeCekanje.Count() != 0))
                {
                    if (brojac1 != 1)
                        brojac1--;
                    textBox5.Text = Convert.ToString(brojac1);
                    textBox6.Text = Convert.ToString(molbeCekanje.Count());
                    textBox1.Text = molbeCekanje[brojac1 - 1].BrojProtokola;
                    textBox2.Text = molbeCekanje[brojac1 - 1].Svrha;
                    textBox3.Text = Convert.ToString((Convert.ToDateTime(molbeCekanje[brojac1 - 1].DatumPodnosenja)).Day + "." + Convert.ToDateTime(molbeCekanje[brojac1 - 1].DatumPodnosenja).Month + "." + Convert.ToDateTime(molbeCekanje[brojac1 - 1].DatumPodnosenja).Year);
                }
                
                if (brojac1 == 1 && molbeCekanje.Count() == 0)
                {
                    textBox6.Text = Convert.ToString(molbeCekanje.Count());
                    label5.Visible = true;
                    button5.Enabled = false;
                    button1.Enabled = false;
                    button2.Enabled = false;
                    textBox5.Text = "0";
                    textBox1.Text = "";
                    textBox2.Text = "";
                    textBox3.Text = "";
                }
            }
        }

        private void Form15_Load(object sender, EventArgs e)
        {
            if (molbeCekanje.Count() != 0)
            {
                button1.Enabled = true;
                button2.Enabled = true;
                label5.Visible = false;
                button5.Enabled = true;
                textBox5.Text = Convert.ToString(brojac1);
                textBox6.Text = Convert.ToString(molbeCekanje.Count());
                textBox1.Text = molbeCekanje[0].BrojProtokola;
                textBox2.Text = molbeCekanje[0].Svrha;
                textBox3.Text = Convert.ToString((Convert.ToDateTime(molbeCekanje[0].DatumPodnosenja)).Day + "." + Convert.ToDateTime(molbeCekanje[0].DatumPodnosenja).Month + "." + Convert.ToDateTime(molbeCekanje[0].DatumPodnosenja).Year);
            }
            else
            {
                label5.Visible = true;
                button5.Enabled = false;
                button1.Enabled = false;
                button2.Enabled = false;
            }
            if (molbeOdobrene.Count() != 0)
            {
                button3.Enabled = true;
                button4.Enabled = true;
                button6.Enabled = true;
                label10.Visible = false;
                textBox8.Text = Convert.ToString(brojac2);
                textBox7.Text = Convert.ToString(molbeOdobrene.Count());
                textBox9.Text = molbeOdobrene[0].BrojProtokola;
                textBox10.Text = molbeOdobrene[0].Svrha;
                textBox11.Text = Convert.ToString((Convert.ToDateTime(molbeOdobrene[0].DatumPodnosenja)).Day + "." + Convert.ToDateTime(molbeOdobrene[0].DatumPodnosenja).Month + "." + Convert.ToDateTime(molbeOdobrene[0].DatumPodnosenja).Year);
            }
            else
            {
                label10.Visible = true;
                button6.Enabled = false;
                button3.Enabled = false;
                button4.Enabled = false;
            }
            if (molbeOdbijene.Count() != 0)
            {
                button8.Enabled = true;
                button9.Enabled = true;
                label13.Visible = false;
                textBox17.Text = Convert.ToString(brojac3);
                textBox18.Text = Convert.ToString(molbeOdbijene.Count());
                textBox13.Text = molbeOdbijene[0].BrojProtokola;
                textBox14.Text = molbeOdbijene[0].Svrha;
                textBox15.Text = Convert.ToString((Convert.ToDateTime(molbeOdbijene[0].DatumPodnosenja)).Day + "." + Convert.ToDateTime(molbeOdbijene[0].DatumPodnosenja).Month + "." + Convert.ToDateTime(molbeOdbijene[0].DatumPodnosenja).Year);
            }
            else
            {
                label13.Visible = true;
                button8.Enabled = false;
                button9.Enabled = false;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (brojac1 != molbeCekanje.Count())
            {
                brojac1++;
                textBox5.Text = Convert.ToString(brojac1);
                textBox1.Text = molbeCekanje[brojac1 - 1].BrojProtokola;
                textBox2.Text = molbeCekanje[brojac1 - 1].Svrha;
                textBox3.Text = Convert.ToString((Convert.ToDateTime(molbeCekanje[brojac1 - 1].DatumPodnosenja)).Day + "." + Convert.ToDateTime(molbeCekanje[brojac1 - 1].DatumPodnosenja).Month + "." + Convert.ToDateTime(molbeCekanje[brojac1 - 1].DatumPodnosenja).Year);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (brojac1 != 1)
            {
                brojac1--;
                textBox5.Text = Convert.ToString(brojac1);
                textBox1.Text = molbeCekanje[brojac1 - 1].BrojProtokola;
                textBox2.Text = molbeCekanje[brojac1 - 1].Svrha;
                textBox3.Text = Convert.ToString((Convert.ToDateTime(molbeCekanje[brojac1 - 1].DatumPodnosenja)).Day + "." + Convert.ToDateTime(molbeCekanje[brojac1 - 1].DatumPodnosenja).Month + "." + Convert.ToDateTime(molbeCekanje[brojac1 - 1].DatumPodnosenja).Year);
            }

        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (brojac2 != molbeOdobrene.Count())
            {
                brojac2++;
                textBox8.Text = Convert.ToString(brojac2);
                textBox9.Text = molbeOdobrene[brojac2 - 1].BrojProtokola;
                textBox10.Text = molbeOdobrene[brojac2 - 1].Svrha;
                if (molbeOdobrene[brojac2 - 1].Tip == 2)
                    button6.Enabled = false;
                else
                    button6.Enabled = true;
                textBox11.Text = Convert.ToString((Convert.ToDateTime(molbeOdobrene[brojac2 - 1].DatumPodnosenja)).Day + "." + Convert.ToDateTime(molbeOdobrene[brojac2 - 1].DatumPodnosenja).Month + "." + Convert.ToDateTime(molbeOdobrene[brojac2 - 1].DatumPodnosenja).Year);
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (brojac2 != 1)
            {
                brojac2--;
                textBox8.Text = Convert.ToString(brojac2);
                textBox9.Text = molbeOdobrene[brojac2 - 1].BrojProtokola;
                textBox10.Text = molbeOdobrene[brojac2 - 1].Svrha;
                if (molbeOdobrene[brojac2 - 1].Tip == 2)
                    button6.Enabled = false;
                else
                    button6.Enabled = true;
                textBox11.Text = Convert.ToString((Convert.ToDateTime(molbeOdobrene[brojac2 - 1].DatumPodnosenja)).Day + "." + Convert.ToDateTime(molbeOdobrene[brojac2 - 1].DatumPodnosenja).Month + "." + Convert.ToDateTime(molbeOdobrene[brojac2 - 1].DatumPodnosenja).Year);
            }

        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (brojac3 != molbeOdbijene.Count())
            {
                brojac3++;
                textBox17.Text = Convert.ToString(brojac3);
                textBox13.Text = molbeOdbijene[brojac3 - 1].BrojProtokola;
                textBox14.Text = molbeOdbijene[brojac3 - 1].Svrha;
                textBox15.Text = Convert.ToString((Convert.ToDateTime(molbeOdbijene[brojac3 - 1].DatumPodnosenja)).Day + "." + Convert.ToDateTime(molbeOdbijene[brojac3 - 1].DatumPodnosenja).Month + "." + Convert.ToDateTime(molbeOdbijene[brojac3 - 1].DatumPodnosenja).Year);
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (brojac3 != 1)
            {
                brojac3--;
                textBox17.Text = Convert.ToString(brojac3);
                textBox13.Text = molbeOdbijene[brojac3 - 1].BrojProtokola;
                textBox14.Text = molbeOdbijene[brojac3 - 1].Svrha;
                textBox15.Text = Convert.ToString((Convert.ToDateTime(molbeOdbijene[brojac3 - 1].DatumPodnosenja)).Day + "." + Convert.ToDateTime(molbeOdbijene[brojac3 - 1].DatumPodnosenja).Month + "." + Convert.ToDateTime(molbeOdbijene[brojac3 - 1].DatumPodnosenja).Year);
            }

        }

        private void button6_Click(object sender, EventArgs e)
        {
            SaveFileDialog spasiMe = new SaveFileDialog();
            spasiMe.Filter = "pdf files (*.pdf)|*.pdf";
            spasiMe.FilterIndex = 2;
            spasiMe.RestoreDirectory = true;
            if (spasiMe.ShowDialog() == DialogResult.OK)
            {
                String s = spasiMe.FileName;
                Thread myWord = new Thread(delegate()
                    {
                        KreirajPotvrdu(conn, s, molbeOdobrene[brojac2 - 1]);
                    });
                myWord.Start();
            }
        }

        private static void KreirajPotvrdu(MySqlConnection myConnection, String myDocument,
                                    PotvrdaMolba myMolba)
        {
            
            Word._Application mWord = new Word.Application();
            Word._Document mDoc = mWord.Documents.Add();
            
            String mySelect = "SELECT * FROM Skola;";
            MySqlCommand myCommand = new MySqlCommand(mySelect, myConnection);
            MySqlDataReader myReader = myCommand.ExecuteReader();

            myReader.Read();

            Word.Paragraph docPar;
            docPar = mDoc.Content.Paragraphs.Add();
            docPar.Range.Text = Convert.ToString(myReader.GetValue(0));
            docPar.Range.Font.Bold = 1;
            docPar.Format.IndentFirstLineCharWidth(1);
            docPar.Format.SpaceAfter = 0;
            docPar.Range.InsertParagraphAfter();

            docPar = mDoc.Content.Paragraphs.Add();
            String mjestoSk = Convert.ToString(myReader.GetValue(2));
            docPar.Range.Text = mjestoSk;
            docPar.Range.InsertParagraphAfter();

            docPar = mDoc.Content.Paragraphs.Add();
            docPar.Range.Text = Convert.ToString(myReader.GetValue(3));
            docPar.Range.InsertParagraphAfter();

            docPar = mDoc.Content.Paragraphs.Add();
            docPar.Range.Text = Convert.ToString(myReader.GetValue(4)) + " "
                                 + Convert.ToString(myReader.GetValue(5));
            docPar.Format.SpaceAfter = 33;
            docPar.Range.InsertParagraphAfter();

            myReader.Close();

            mySelect = "SELECT * FROM PotvrdePoUceniku WHERE BorjProtokola='"
                        + myMolba.BrojProtokola + "';";
            myCommand = new MySqlCommand(mySelect, myConnection);
            myReader = myCommand.ExecuteReader();

            myReader.Read();

            docPar = mDoc.Content.Paragraphs.Add();
            docPar.Range.Text = "Broj Protokola: " +
                Convert.ToString(myReader.GetValue(0));
            docPar.Range.Font.Bold = 0;
            docPar.Range.InsertParagraphAfter();

            docPar = mDoc.Content.Paragraphs.Add();
            docPar.Range.Text = "P O T V R D A";
            docPar.Range.Font.Bold = 1;
            docPar.Range.Font.Size = 20;
            docPar.Range.ParagraphFormat.Alignment =
                Word.WdParagraphAlignment.wdAlignParagraphCenter;
            docPar.Range.InsertParagraphAfter();

            docPar = mDoc.Content.Paragraphs.Add();
            String ime = Convert.ToString(myReader.GetValue(6));
            String imeS = Convert.ToString(myReader.GetValue(10));
            String prezimeS = Convert.ToString(myReader.GetValue(11));
            String prezime = Convert.ToString(myReader.GetValue(7));
            DateTime tmp = Convert.ToDateTime(myReader.GetValue(8));
            String datumR = Convert.ToString(tmp.Day) + "." +
                            Convert.ToString(tmp.Month) + "." +
                            Convert.ToString(tmp.Year) + ".g.";
            String jmbg = Convert.ToString(myReader.GetValue(5));
            String mjestoR = Convert.ToString(myReader.GetValue(9));
            String razred = Convert.ToString(myReader.GetValue(12));
            String odjeljenje = Convert.ToString(myReader.GetValue(13));
            String skGodina = Convert.ToString(myReader.GetValue(14));
            String svrha = Convert.ToString(myReader.GetValue(4));
            docPar.Range.Text = ime + " (" + imeS + " " + prezimeS + ") " +
                prezime + ", rodjen-a " + datumR + " jmbg " + jmbg + " mjesto " +
                mjestoR + ", redovno pohadja " + razred + ". razred odjeljenje " +
                odjeljenje + " u skolskoj " + skGodina + " godini. Ova potvrda " +
                "izdaje se u svrhu " + svrha + " te se u druge svrhe ne moze koristiti.";
            docPar.Range.Font.Bold = 0;
            docPar.Range.Font.Size = 12;
            docPar.Range.ParagraphFormat.Alignment =
                Word.WdParagraphAlignment.wdAlignParagraphJustify;
            docPar.Format.SpaceAfter = 50;
            docPar.Range.InsertParagraphAfter();

            docPar = mDoc.Content.Paragraphs.Add();
            docPar.Range.Text = "____________________________";
            docPar.Format.SpaceAfter = 0;
            docPar.Range.ParagraphFormat.Alignment =
                Word.WdParagraphAlignment.wdAlignParagraphRight;
            docPar.Range.InsertParagraphAfter();

            docPar = mDoc.Content.Paragraphs.Add();
            docPar.Range.Text = "Potpis direktora";
            docPar.Format.RightIndent = 45;
            docPar.Format.SpaceAfter = 20;
            docPar.Range.InsertParagraphAfter();

            tmp = Convert.ToDateTime(myReader.GetValue(1));
            String datumP = Convert.ToString(tmp.Day) + "." +
                            Convert.ToString(tmp.Month) + "." +
                            Convert.ToString(tmp.Year) + ".g.";
            docPar = mDoc.Content.Paragraphs.Add();
            docPar.Range.Text = mjestoSk + ", " + datumP;
            docPar.Format.RightIndent = 0;
            docPar.Range.ParagraphFormat.Alignment =
                Word.WdParagraphAlignment.wdAlignParagraphCenter;
            docPar.Range.InsertParagraphAfter();

            try
            {
                mDoc.SaveAs(myDocument, Word.WdSaveFormat.wdFormatPDF);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            myReader.Close();
            mWord.Quit(false);
            System.Diagnostics.Process.Start(@myDocument);
        }

    }
}
